package com.tdly.buduizhong.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tdly.buduizhong.R;
import com.tdly.buduizhong.base.BaseActivity;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

public class SaveActivity extends BaseActivity implements View.OnClickListener {
    private Context context = this;
    private LinearLayout et_save_name;
    private EditText editText;
    private TextView textView2,textView3;
    private Button bt_save_cancel,bt_save_ok;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_save);

        initView();
    }

    private void initView() {
        et_save_name=findViewById(R.id.et_save_name);
        editText=findViewById(R.id.editText);
        textView2=findViewById(R.id.textView2);
        textView3=findViewById(R.id.textView3);
        bt_save_cancel=findViewById(R.id.bt_save_cancel);
        bt_save_ok=findViewById(R.id.bt_save_ok);

        bt_save_cancel.setOnClickListener(this);
        bt_save_ok.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_save_ok:
                Editable name = editText.getText();
                Log.e("!@#!@#@!",name.toString());
                String str=getIntent().getStringExtra("str");
                String[] tmp = str.trim().split(",");
                String data="";
                for (int i = 0; i + 1 < tmp.length ; i += 2) {
                    int num = StrToInt(tmp[i + 1]) * 256 + StrToInt(tmp[i]);
                    data=data+num+"\t";
                }
                Log.e("data",data);
                try {
                    savaFileToSD(name.toString(),data);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Toast.makeText(context,"保存成功",Toast.LENGTH_SHORT).show();
                finish();
                break;
            case R.id.bt_save_cancel:
                finish();
                break;
        }
    }

    //往SD卡写入文件的方法
    public void savaFileToSD(String filename, String filecontent) throws Exception {
        //如果手机已插入sd卡,且app具有读写sd卡的权限
        File Folder = new File(Environment.getExternalStorageDirectory().getCanonicalPath(), "tdly");
        if (!Folder.exists())//判断文件夹是否存在，不存在则创建文件夹，已经存在则跳过
        {

            Folder.mkdir();//创建文件夹

            //两种方式判断文件夹是否创建成功
            //Folder.isDirectory()返回True表示文件路径是对的，即文件创建成功，false则相反
            boolean isFilemaked1 = Folder.isDirectory();
            //Folder.mkdirs()返回true即文件创建成功，false则相反
            boolean isFilemaked2 = Folder.mkdirs();

            if (isFilemaked1 || isFilemaked2) {
                Log.i("123123","创建文件夹成功");
            } else {
                Log.i("123123","创建文件夹失败");
            }

        }else {
            Log.i("123123","文件夹已存在");
        }
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            filename = Environment.getExternalStorageDirectory().getCanonicalPath() + "/tdly/" + filename+".txt";
            Log.e("!@#!@#!$",filename);
            //这里就不要用openFileOutput了,那个是往手机内存中写数据的
            FileOutputStream output = new FileOutputStream(filename);
            output.write(filecontent.getBytes());
            //将String字符串以字节流的形式写入到输出流中
            output.close();
            //关闭输出流
        } else {
            Toast.makeText(context, "SD卡不存在或者不可读写", Toast.LENGTH_SHORT).show();
        }
    }
}
